from flask import Flask
from flask import Flask, request, render_template
import time
import os
import cv2
import pickle

from PIL import Image

app = Flask(__name__)

save_dir = "./flask-save-dir"

@app.route("/")
def index():
    return "Hello World!"

@app.route('/receive-frame', methods=['POST'])
def handler_receive_frame():
    timestamp = str(int(time.time()))
    file = request.files['image']
    file_path = os.path.join(save_dir, "image_" + timestamp + ".png")
    # file.save(file_path)
    # cv2.imwrite(file_path, file.read())
    image_numpy = pickle.loads(file.read())
    cv2.imwrite(filename=file_path, img=image_numpy)

    print("receive file name ", file.filename)
    print("output_file_name ", file_path)
    result = {"error": 0}
    return result


if __name__ == "__main__":
    app.run(port=5000)

